package com.javabi.htmlbuilder.html.element.table;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.javabi.htmlbuilder.html.HTMLBuilder;
import com.javabi.htmlbuilder.html.HTMLElement;
import com.javabi.htmlbuilder.html.HTMLElementName;

/**
 * An HTML Table Element.
 * <p>
 * &lt;table&gt; ... &lt;/table&gt;
 * </p>
 */
public class Table extends HTMLElement<Table> implements Iterable<Row> {

	/** The row list. */
	private final List<Row> rowList = new ArrayList<Row>();
	/** The cell spacing. */
	private int cellSpacing = 0;
	/** The cell padding. */
	private int cellPadding = 0;
	/** The border. */
	private int border = 0;

	/**
	 * Returns a new table row.
	 * @return a new table row.
	 */
	public Row tr() {
		Row row = new Row();
		rowList.add(row);
		return row;
	}

	/**
	 * Returns a new table row.
	 * @return a new table row.
	 */
	public Row row() {
		return tr();
	}

	/**
	 * Returns the number of rows.
	 * @return the number of rows.
	 */
	public int rows() {
		return rowList.size();
	}

	/**
	 * Returns true if this table is empty.
	 * @return true if this table is empty.
	 */
	public boolean isEmpty() {
		return rowList.isEmpty();
	}

	/**
	 * Returns an existing table row.
	 * @param index the index.
	 * @return an existing table row.
	 */
	public Row getRow(int index) {
		return rowList.get(index);
	}

	/**
	 * Returns the row iterator.
	 * @return the row iterator.
	 */
	public Iterator<Row> iterator() {
		return rowList.iterator();
	}

	/**
	 * Returns the cellSpacing
	 * @return the cellSpacing
	 */
	public int getCellSpacing() {
		return cellSpacing;
	}

	/**
	 * Sets the cellSpacing
	 * @param cellSpacing the cellSpacing to set
	 */
	public void setCellSpacing(int cellSpacing) {
		if (cellSpacing < 0) {
			throw new IllegalArgumentException("cellSpacing=" + cellSpacing);
		}
		this.cellSpacing = cellSpacing;
	}

	/**
	 * Returns the cellPadding
	 * @return the cellPadding
	 */
	public int getCellPadding() {
		return cellPadding;
	}

	/**
	 * Sets the cellPadding
	 * @param cellPadding the cellPadding to set
	 */
	public void setCellPadding(int cellPadding) {
		if (cellPadding < 0) {
			throw new IllegalArgumentException("cellPadding=" + cellPadding);
		}
		this.cellPadding = cellPadding;
	}

	/**
	 * Returns the border
	 * @return the border
	 */
	public final int getBorder() {
		return border;
	}

	/**
	 * Sets the border
	 * @param border the border to set
	 */
	public final void setBorder(int border) {
		if (border < 0) {
			throw new IllegalArgumentException("border=" + border);
		}
		this.border = border;
	}

	/**
	 * Returns true to format this element.
	 * @return true to format this element.
	 */
	public boolean format() {
		return true;
	}

	/**
	 * Returns the element name.
	 * @return the element name.
	 */
	public HTMLElementName getElementName() {
		return HTMLElementName.TABLE;
	}

	/**
	 * Append this to the given html builder.
	 * @param html the html builder.
	 * @return the html builder.
	 */
	public HTMLBuilder appendTo(HTMLBuilder html) {
		if (!isEmpty()) {
			html.open(this, true);
			appendInlineStyleAttributesTo(html);
			if (border > 0) {
				html.attribute("border", border);
			}
			if (cellSpacing > 0) {
				html.attribute("cellspacing", cellSpacing);
			}
			if (cellSpacing > 0) {
				html.attribute("cellpadding", cellPadding);
			}
			for (Row row : rowList) {
				row.appendTo(html);
			}
			html.close(this);
		}
		return html;
	}

}
